package com.zzh.myapplication.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.icarplus.car.R;
import net.icarplus.car.bean.MyOrderListBean;
import net.icarplus.car.views.PinnedHeaderListView;

import java.util.List;

public class CustomAdapter extends BaseAdapter
        implements OnScrollListener, PinnedHeaderListView.PinnedHeaderAdapter {

    private static final String TAG = CustomAdapter.class.getSimpleName();
    private Context mContext;
    private List<MyOrderListBean> mData;

    public CustomAdapter(Context pContext, List<MyOrderListBean> pData) {
        mContext = pContext;
        mData = pData;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            convertView = View.inflate(mContext, R.layout.item_car_order, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MyOrderListBean model = (MyOrderListBean) getItem(position);
        viewHolder.carPic.setImageResource(model.getCarPic());
        viewHolder.orderNum.setText(model.getOrderNum());
        viewHolder.orderCarNum.setText(model.getOrderCarNum());
        viewHolder.orderCarName.setText(model.getOrderCarName());
        viewHolder.orderStatus.setText(model.getOrderStatus());
        if (needTitle(position)) {
            viewHolder.title.setText(model.getTitle());
            viewHolder.title.setVisibility(View.VISIBLE);
        } else {
            viewHolder.title.setVisibility(View.GONE);
        }
        return convertView;
    }

    @Override
    public int getCount() {
        if (null != mData) {
            return mData.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (null != mData && position < getCount()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        if (view instanceof PinnedHeaderListView) {
            ((PinnedHeaderListView) view).controlPinnedHeader(firstVisibleItem);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    @Override
    public int getPinnedHeaderState(int position) {
        if (getCount() == 0 || position < 0) {
            return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_GONE;
        }
        if (isMove(position) == true) {
            return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP;
        }
        return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_VISIBLE;
    }

    @Override
    public void configurePinnedHeader(View headerView, int position, int alpaha) {
        MyOrderListBean itemEntity = (MyOrderListBean) getItem(position);
        String headerValue = itemEntity.getTitle();
        if (!TextUtils.isEmpty(headerValue)) {
            TextView headerTextView = (TextView) headerView.findViewById(R.id.header);
            headerTextView.setText(headerValue);
        }
    }

    private boolean needTitle(int position) {
        if (position == 0) {
            return true;
        }
        if (position < 0) {
            return false;
        }
        MyOrderListBean currentEntity = (MyOrderListBean) getItem(position);
        MyOrderListBean previousEntity = (MyOrderListBean) getItem(position - 1);
        if (null == currentEntity || null == previousEntity) {
            return false;
        }
        String currentTitle = currentEntity.getTitle();
        String previousTitle = previousEntity.getTitle();
        if (null == previousTitle || null == currentTitle) {
            return false;
        }
        if (currentTitle.equals(previousTitle)) {
            return false;
        }
        return true;
    }

    private boolean isMove(int position) {
        MyOrderListBean currentEntity = (MyOrderListBean) getItem(position);
        MyOrderListBean nextEntity = (MyOrderListBean) getItem(position + 1);
        if (null == currentEntity || null == nextEntity) {
            return false;
        }
        String currentTitle = currentEntity.getTitle();
        String nextTitle = nextEntity.getTitle();
        if (null == currentTitle || null == nextTitle) {
            return false;
        }
        if (!currentTitle.equals(nextTitle)) {
            return true;
        }
        return false;
    }

    class ViewHolder {
        ImageView carPic;
        TextView orderNum;
        TextView orderCarNum;
        TextView orderCarName;
        TextView orderStatus;
        TextView title;

        public ViewHolder(View view) {
            carPic = (ImageView) view.findViewById(R.id.carPic);
            orderNum = (TextView) view.findViewById(R.id.orderNum);
            orderCarNum = (TextView) view.findViewById(R.id.orderCarNum);
            orderCarName = (TextView) view.findViewById(R.id.orderCarName);
            orderStatus = (TextView) view.findViewById(R.id.orderStatus);
            title = (TextView) view.findViewById(R.id.title);
        }
    }

}
